class ComicResourceManager {

	private static _instance:ComicResourceManager;
	public static get instance():ComicResourceManager
	{
		if(ComicResourceManager._instance == null)
		{
			ComicResourceManager._instance = new ComicResourceManager();
		}
		return ComicResourceManager._instance
	}
	private _config:ResourceConfig;
	private _movieConfig;
	private _movieVO:ComicMovieVO;
	private _loadRoleComplete:boolean;
	private _dbConfigMap:any;

	public constructor() {
		this._dbConfigMap = {};
	}
	
	public async initConfig(allConfig:string, resourceConfig:string, ):Promise<void> {
        if(this._config == null)
		{
			await RES.loadGroup("config");
			let resConfig:ResourceConfig = RES.getRes("resource_config_json");
			this._config = resConfig;
			this._movieConfig = RES.getRes("comicMovieConfig_json");
            this._movieVO = new ComicMovieVO(this._movieConfig);
		}

		this.init();
	}
	public get movieVO():ComicMovieVO
	{
		return this._movieVO;
	}
	private init():void
	{

	}

	public async loadRoleAsset(roleAsset:string,shotAsset:string, clothAsset:string):Promise<void>{
		if(!this._loadRoleComplete)
		{
			await RES.loadGroup("role");
			await RES.loadGroup("shot");
			await RES.loadGroup("cloth");

			this._loadRoleComplete = true;
		}
	}

	public loadDBConfig(factory:dragonBones.EgretFactory, dbConfig:DBConfig):void
	{
		if(this._dbConfigMap[dbConfig.name] == null)
		{
			factory.parseDragonBonesData(RES.getRes(dbConfig.dbData)).autoSearch = true;
        	factory.parseTextureAtlasData(RES.getRes(dbConfig.textureData),
		                              RES.getRes(dbConfig.texturePng)).autoSearch = true;
			this._dbConfigMap[dbConfig.name] = true;
		}
	}
	public getDragonBonesAsset(dragonBonesName:string):DBConfig
	{
		let i:number;
		let len:number;
		let j:number;
		let jlen:number;
		let db:DB;

		for(i = 0, len = this._config.dbs.length; i < len; i++)
		{
			db = this._config.dbs[i];
			if(db.name == dragonBonesName)
			{
				return db.asset;
			}
		}
		return null;
	}

	public getArmatureAsset(armatureName:string):DBConfig
	{
		let i:number;
		let len:number;
		let j:number;
		let jlen:number;
		let db:DB;

		for(i = 0, len = this._config.dbs.length; i < len; i++)
		{
			db = this._config.dbs[i];
			for(j = 0, jlen = db.armatures.length; j < jlen; j++)
			{
				if(db.armatures[j] == armatureName)
				{
					return db.asset;
				}
			}
		}
		return null;
	}

	public getSuit(suitName:string):Suit
	{
		let i:number;
		let len:number;

		for(i = 0, len = this._config.suits.length; i < len; i++)
		{
			if(this._config.suits[i].name == suitName)
			{
				return this._config.suits[i];
			}
		}
		return null;
	}

	public getCloth(clothName:string):Cloth
	{
		let i:number;
		let len:number;

		for(i = 0, len = this._config.clothes.length; i < len; i++)
		{
			if(this._config.clothes[i].name == clothName)
			{
				return this._config.clothes[i];
			}
		}
		return null;
	}
}